package org.autumn.lock;

import java.util.concurrent.locks.LockSupport;

public class AlternateExecution {


    static Thread t1;
    static Thread t2;
    static Thread t3;

    // 交替执行
    public static void main(String[] args) {
        ParkUnPark parkUnPark = new ParkUnPark();

        t1 = new Thread(() -> {
            parkUnPark.print("a", t2);
        }, "t1");
        t2 = new Thread(() -> {
            parkUnPark.print("b", t3);
        }, "t2");
        t3 = new Thread(() -> {
            parkUnPark.print("c", t1);
        }, "t3");

        t1.start();
        t2.start();
        t3.start();

        LockSupport.unpark(t1);

    }
}

class ParkUnPark {
    public void print(String str, Thread next) {
        LockSupport.park();
        System.out.println(str);
        LockSupport.unpark(next);
    }
}